# -*- coding: utf-8 -*-
import datetime
import logging
import random
import decimal
import time


class DBUntilsTools:
    def __init__(self):
        pass

    def get_random(self, min_val, max_val):
        val = random.randint(min_val, max_val)
        return val

    def get_random_float(self, min_val, max_val):
        val = random.uniform(min_val, max_val)
        return val

    def get_random_string(self, length):
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        val = ''.join(random.choice(chars) for _ in range(length))
        return str(val)

    def get_incre_time(self):
        val = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
        return val

    def get_random_date(self):
        tmp = self.make_random_time()
        val = tmp.strftime("%Y-%m-%d")
        return val

    def get_current_date(self):
        tmp = self.make_random_time()
        val = tmp.strftime("%Y-%m-%d")
        return val

    def set_return_massage(self, res, only_data=None):
        try:
            if only_data:
                return res["data"]
            else:
                return res
        except Exception as e:
            res['message'] == "fail"
            res['data'] == str(e)
        return res

    def get_random_time(self):
        # 获取当前时间戳
        end_timestamp = int(time.time())
        # 定义起始时间戳
        start_timestamp = 0  # Unix 纪元开始时间戳

        # 生成随机时间戳
        random_timestamp = random.randrange(start_timestamp, end_timestamp)
        random_time = time.strftime("%H:%M:%S", time.localtime(random_timestamp))
        return random_time  # 返回格式为 'HH:MM:SS'，不再加单引号

    def get_random_datetime(self):
        tmp = self.make_random_time()
        val = tmp.strftime("%Y-%m-%d %H:%M:%S")
        return val

    def get_random_timestamp(self):
        val = str(datetime.datetime.now().timestamp())
        return val

    def make_random_time(self):
        start_date = datetime.datetime(1900, 1, 1)
        end_date = datetime.datetime.now()

        time_between_dates = end_date - start_date
        days_between_dates = time_between_dates.days
        random_number_of_days = random.randrange(days_between_dates)
        random_datetime = start_date + datetime.timedelta(days=random_number_of_days)

        # 生成随机的小时、分钟和秒
        random_hour = random.randint(0, 23)
        random_minute = random.randint(0, 59)
        random_second = random.randint(0, 59)

        random_datetime = random_datetime.replace(hour=random_hour, minute=random_minute, second=random_second)
        return random_datetime

    def get_random_id(self):
        return random.randint(0, 2 ** 31 - 1)

    def get_random_decimal(self, max_integer_digits, max_fraction_digits):
        # 设置decimal精度
        decimal.getcontext().prec = max_integer_digits + max_fraction_digits
        # 生成整数部分
        integer_part = random.randint(0, 10 ** max_integer_digits - 1)
        # 生成小数部分
        fraction_part = random.randint(0, 10 ** max_fraction_digits - 1)
        # 组合整数和小数部分
        decimal_value = decimal.Decimal(f'{integer_part}.{fraction_part:0{max_fraction_digits}d}')
        return decimal_value


if __name__ == '__main__':
    # a = BaseTool()
    # b = a.get_incre_time()
    # print(b)
    a = DBUntilsTools()
    b1 = a.get_random_datetime()
    b2 = a.get_random_time()
    b3 = a.get_random_date()
    print(b1, b2, b3)
    pass
